﻿/*
Copyright (c) 2009 Vratislav Cermak (vratacermak@gmail.com)
 
This file is part of TeachMe Project. See http://code.google.com/p/teach-me for more information.
 
TeachMe is an open source project. Using of TeachMe is free of charge.
It is distributed under GNU General Public License version 3 (GPLv3).
GPLv3 can be found at http://www.gnu.org/licenses/gpl.html.
 
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the license for more details.
*/

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Xml.Serialization;

namespace TeachMe.Application.Configuration
{
    public class MachineSettings : Checkable
    {
        private string _path;

        private List<WindowProportions> _windowsProportions;
        private List<ListViewColumnsProportions> _columnsProportions;
        private List<GridProportions> _gridsProportions;
        private string _lastBrowseDirectory;
        private string _lastEditedDictionaryPath;
        private string _entryDisplayedSoundPath;

        [XmlElement(ElementName = "LastBrowseDirectory")]
        public string XmlLastBrowseDirectory
        {
            get
            {
                return _lastBrowseDirectory;
            }
            set
            {
                _lastBrowseDirectory = value;
            }
        }

        [XmlIgnore]
        public string LastBrowseDirectory
        {
            get
            {
                return PathsProvider.MakeTransient(_lastBrowseDirectory);
            }
            set
            {
                _lastBrowseDirectory = PathsProvider.MakePersistent(value);
            }
        }

        [XmlElement(ElementName = "EntryDisplayedSoundPath")]
        public string XmlEntryDisplayedSoundPath
        {
            get
            {
                return _entryDisplayedSoundPath;
            }
            set
            {
                _entryDisplayedSoundPath = value;
            }
        }

        [XmlIgnore]
        public string EntryDisplayedSoundPath
        {
            get
            {
                return PathsProvider.MakeTransient(_entryDisplayedSoundPath);
            }
            set
            {
                _entryDisplayedSoundPath = PathsProvider.MakePersistent(value);
            }
        }

        [XmlElement(ElementName = "LastEditedDictionaryPath")]
        public string XmlLastEditedDictionaryPath
        {
            get
            {
                return _lastEditedDictionaryPath;
            }
            set
            {
                _lastEditedDictionaryPath = value;
            }
        }

        [XmlIgnore]
        public string LastEditedDictionaryPath
        {
            get
            {
                return PathsProvider.MakeTransient(_lastEditedDictionaryPath);
            }
            set
            {
                _lastEditedDictionaryPath = PathsProvider.MakePersistent(value);
            }
        }

        public List<ListViewColumnsProportions> ColumnsProportions
        {
            get { return _columnsProportions ?? (_columnsProportions = new List<ListViewColumnsProportions>()); }
            set 
            { 
                _columnsProportions = value; 
            }
        }

        public List<GridProportions> GridsProportions
        {
            get
            {
                return _gridsProportions ?? (_gridsProportions = new List<GridProportions>());
            }
            set
            {
                _gridsProportions = value;
            }
        }

        public int LastSettingsPageIndex { get; set; }

        public List<WindowProportions> WindowsProportions
        {
            get { return _windowsProportions ?? (_windowsProportions = new List<WindowProportions>()); }
            set { _windowsProportions = value; }
        }

        public Point FirstEntryLocation { get; set; }

        public DateTime LastNewVersionCheck { get; set; }

        public string LastOptionsNode { get; set; }

        public static MachineSettings Load(string path, out bool created)
        {
            created = !File.Exists(path);

            MachineSettings machineSettings = null;

            if (File.Exists(path))
            {
                Logging.Debug(typeof(MachineSettings), "Loading machine settings from '{0}'.", path);

                try
                {
                    XmlSerializer serializer = new XmlSerializer(typeof (MachineSettings));
                    using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read))
                    {
                        machineSettings = serializer.Deserialize(stream) as MachineSettings;
                    }
                }
                catch(Exception e)
                {
                    Logging.Error(typeof(MachineSettings), e, "Machine settings could not be read.", path);
                }
            }
            if (machineSettings == null)
            {
                Logging.Debug(typeof(MachineSettings), "Default machine settings will be created.", path);

                machineSettings = new MachineSettings();
            }

            machineSettings._path = path;

            return machineSettings;
        }

        protected override void Save()
        {
            try
            {
                Logging.Debug(GetType(), "Saving machine settings to '{0}'.", _path);

                XmlSerializer serializer = new XmlSerializer(typeof(MachineSettings));
                using (FileStream stream = new FileStream(_path, FileMode.Create, FileAccess.Write))
                {
                    serializer.Serialize(stream, this);
                }
            }
            catch (Exception e)
            {
                Logging.Error(GetType(), e, "Machine settings could not be saved to '{0}'.", _path);
            }
        }
    }
}
